const app = getApp()
Page({
  data: {
    type: '',
    dept: '',
    _id: '',
    name: '',
    sex: '',
    phone: '',
    stuClass: '',
    teaPost: '',
    disabled: false
  },
  onLoad: function(options) {
    this.setData({
      type: options.type,
      dept: app.globalData.userDept
    })
  },
  inpId: function(e) {
    this.setData({
      _id: e.detail.value
    })
  },
  inpName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  inpSex: function(e) {
    this.setData({
      sex: e.detail.value
    })
  },
  inpPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inpClass: function(e) {
    this.setData({
      stuClass: e.detail.value
    })
  },
  inpPost: function(e) {
    this.setData({
      teaPost: e.detail.value
    })
  },
  funAddUser: function() {
    const that = this
    that.setData({
      disabled: true
    })
    if (that.data._id == '') {
      if (that.data.type == 'stu') {
        wx.showToast({
          title: '请输入学号',
          icon: 'none',
          duration: 2000
        })
      } else if (that.data.type == 'tea') {
        wx.showToast({
          title: '请输入导师工号',
          icon: 'none',
          duration: 2000
        })
      }
      that.setData({
        disabled: false
      })
    } else if (that.data.name == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        disabled: false
      })
    } else {
      wx.cloud.callFunction({
        name: 'addUser',
        data: {
          type: that.data.type,
          dept: that.data.dept,
          _id: that.data._id,
          name: that.data.name,
          sex: that.data.sex,
          phone: that.data.phone,
          stuClass: that.data.stuClass,
          teaPost: that.data.teaPost
        }
      }).then(res => {
        if (res.result.result == 1) {
          wx.showToast({
            title: '添加成功！',
            icon: 'success',
            duration: 2000
          })
        } else if (res.result.result == 2) {
          wx.showToast({
            title: '添加失败！该学号（工号）已存在',
            icon: 'none',
            duration: 2000
          })
        } else if (res.result.result == 3) {
          wx.showToast({
            title: '添加失败！',
            icon: 'none',
            duration: 2000
          })
        }
        that.setData({
          disabled: false
        })
      })
    }
  }
})